<?php
/*************************************************************************************
 *
 *	FileName: EventDispatcher.php
 *	Desc    : Process the scores of the user
 *	Author  : Seal
 *	Date    : 2009-11-30
 *
 *************************************************************************************/


/*************************************************************************************
 *
 * 							===>>	NOTICE!!	<<===
 * 
 *	Every event has a constant code which is defined in the /config/EventCode.php as 
 *	a constant. Therefore if you want to add a new event, firstly define a constant 
 *	for this event and then add codes in the Notify function below.
 *
 *************************************************************************************/


//注册	                +50	自动
//帐号邮件认证	+3	自动（没有认证不能点评，这块目前暂略）
//完善资料	        +5	自动
//首次上传头像	+10	自动
//邀请好友成功	+5	自动每天上限10分
//每次登陆	        +1	自动每天上限5分
//添加店铺           +3   自动每天上限15分
//添加商品           +3   自动每天上限15分
//发表评单           +5   自动
//发表点评           +3     如果加商品另外+3分，加图片另外+2分(第一点评加3分

class EventDispatcher
{
	private $userDAO;
	//private $shopDAO;
	
	/**********************************************************
	 *
	 * Attention! Do not add quotation on the key!!
	 *
	 **********************************************************/
	private $scores = array(
			REGISTER =>'50',  //
			UPLOAD_IMAGE_FIRST_TIME=>'10',  //
			INVITE_FRIEND_SUCCESS=>'10',   //
			LOGIN =>'0',
			CREATE_LIST =>'10',    //
			FILL_DETAIL_INFO => '40', 
		
			FIRST_REVIEW          =>'3',    //
			REVIEW_INCLUDE_PRODUCT =>'5',   //
			REVIEW_INCLUDE_PIC =>'3',    //
			NORMAL_REVIEW =>'5',    //
	
			ADD_SHOP =>'3',   //
			ADD_PRODUCT =>'1',   //
			ADD_FRIEND=>'0',
			COLLECT_SHOP=>'0',
			COLLECT_PRODUCT=>'0',
	
			REVIEW_IMPEACH=>'5',   //
			MODIFY_SHOP=>'2',   //
			MODIFY_PRODUCT=>'2',   //
			
			FAKE_REVIEW=>'-10'
	);
	
 	public function __construct()
    {
    	$this->userDAO = GetUserDAO();
    	//$this->shopDAO = GetShopDAO();
    }
    
    /*
     * caution:
     * $para is an array
     * uid-> id of user
     * sid-> id of shop
     * pid-> id of product. If more than one product, place ':' between the id of product, e.g: 1:2:3
     * lid-> id of list
     */
    
    /**********************************************************
     *
     * 	DONOT change the case statement like this: 
     * 			case var1 || var 2 || var3
     *  It's wrong in syntax and never works!!!!!!!!!
     *
     *	by yawei
     *
     **********************************************************/
	public function Notify( $uid, $event, $para = NULL )
	{
		//review event is array
		if(is_array($event))
		{
			$score = 0;
			foreach($event as $k=>$v)
 			{
 				$score = $score + $this->scores["$v"];
 			}
 			
			$this->AddUserEvent( $uid, $event, $para);
			$this->AddContribution( $uid, $score, NORMAL_REVIEW);
			
		}
		else
		{
				//other event is string
				switch($event)
				{
					case LOGIN:
					{
						$this->AddContribution( $uid, $this->scores[$event], $event );
						break;
					}
					case REGISTER:
					{
						$this->AddContribution( $uid, $this->scores[$event], $event );
						$this->AddUserEvent($uid, $event, $para);
						break;
					}
					case INVITE_FRIEND_SUCCESS:
					{
						$this->AddContribution( $uid, $this->scores[$event],$event );
						$this->AddUserEvent($uid, $event, $para);
						break;
					}		
					case UPLOAD_IMAGE_FIRST_TIME:
					{
						$this->AddContribution( $uid, $this->scores[$event], $event );
						break;
					}
					case FILL_DETAIL_INFO:
					{
						$this->AddContribution( $uid, $this->scores[$event], $event );
						break;
					}
					case ADD_SHOP:
					{
						$this->AddContribution( $uid, $this->scores[$event], $event  );
						$this->AddUserEvent( $uid, $event, $para );
						break;
					}
					case ADD_PRODUCT:
					{
						$this->AddContribution( $uid, $this->scores[$event], $event  );
						$this->AddUserEvent( $uid, $event, $para );
						break;
					}
					case MODIFY_SHOP:
					{
					//	$this->AddContribution( $uid,  $this->scores[$event] );
					//  后台管理员处加分，不自动加分
						$this->AddUserEvent( $uid, $event, $para );
						break;
					}
					case MODIFY_PRODUCT:
					{
					//	$this->AddContribution( $uid, $this->scores[$event] );
					//  后台管理员处加分，不自动加分
						break;
					}
					case CREATE_LIST:
					{
						$this->AddContribution( $uid, $this->scores[$event], $event );
						$this->AddUserEvent( $uid, $event, $para );
						break;	
					}
					case REVIEW_IMPEACH:
					{
						$this->AddContribution( $uid, $this->scores[$event], $event );
						break;
					}	
					case ADD_FRIEND:
					{
						$this->AddUserEvent($uid, $event, $para);
						break;
					}
					case COLLECT_SHOP:
					{
						$this->AddUserEvent($uid, $event, $para);
						break;
					}
					case COLLECT_PRODUCT:
					{
						$this->AddUserEvent($uid, $event, $para);
						break;
					}
					case COLLECT_LIST:
					{
						$this->AddUserEvent( $uid, $event, $para);
						break;
					}
					case FAKE_REVIEW:
					{
						$this->AddContribution( $uid, $this->scores[$event], $event );
						break;
					}
					default:
						break;
				}				
			
		}
		

	}

	
	/**********************************************************
	 *
	 * functions used to add the contribution score and events
	 *
	 **********************************************************/
	private function AddContribution( $uid, $score , $event=null)
	{
		if($this->userDAO->IsScoreLimited($uid, $event)==false)
		{
			$this->userDAO->ModifyScoreUpper( $uid, $event );
			$this->userDAO->ModifyScores( $uid, $score );
			if( $event!= INVITE_FRIEND_SUCCESS&&$score>'0')
			{
				$msg = '恭喜您获得了' . $score . '点贡献值和' . $score . '个E币！';
				edp_setsession( 'MSG', $msg );				
			}
		}
	}
	
	private function AddUserEvent( $uid, $event, $parameters )
	{
		$this->userDAO->AddNewEvent( $uid, $event, $parameters, GetCurrentDate() );
	}
	
}

?>